<?xml version="1.0"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <fingerprint>
      <xsl:variable name="docelem" select="/*"/>
      <xsl:for-each select="distinct-values(//namespace::*)">
        <xsl:sort/>
        <xsl:variable name="uri" select="."/>
        <xsl:message>Processing namespace <xsl:value-of select="$uri"/></xsl:message>
        <namespace name="{.}">
          <xsl:variable name="all-elements-in-ns">
            <xsl:for-each select="$docelem//*[namespace-uri()=$uri]">
              <e>
                <xsl:value-of select="local-name(.)"/>
              </e>
            </xsl:for-each>
          </xsl:variable>
          <xsl:for-each select="distinct-values($all-elements-in-ns//*)">
            <xsl:sort/>
            <xsl:variable name="lname" select="."/>
            <element name="{$lname}"
              count="{count($docelem//*[local-name()=$lname and namespace-uri()=$uri])}"/>
          </xsl:for-each>
        </namespace>
      </xsl:for-each>
    </fingerprint>
  </xsl:template>
</xsl:transform>
